< previous page page_249 next page >

Page 249
Four types of logical statements define the condition that you can use with the WHERE clause:
A comparison is used to compare a field to another field or a given value (for example, [Sales Quantity] > 10).
LIKE compares a field to a specified pattern (for example, SM*).
IN is used to compare a field to a list of acceptable values (for example, State IN (NY, NJ, CT)).
BETWEEN is used to compare a field to a value range (for example, [Order Date]BETWEEN 01/01/96 and 02/28/96).
Each predicate has many different options and wildcard values that you can use. Because some of them vary depending on the database, I suggest that you review these options for the database that you're working with.
The WHERE clause lets you specify multiple conditions to filter on more than one field at a time. Each individual condition follows the syntax discussed earlier but are combined by using the logical operators AND and OR. By using multiple-condition statements, you can find all the contacts in New York and New Jersey, or you can find anyone whose first or last name begins with Rich:
SELECT * FROM Contact WHERE State IN (NY, NJ) or
     ([Last Name] LIKE RICH* OR [First Name] = LIKE RICH*)
In addition to specifying the records to be retrieved, you can also use the SELECT statement to specify the order that you want the records to appear in the dynaset. To sort the records, you would use the ORDER BY clause of the SELECT statement. You can specify the sort order with a single field or with multiple fields. If you use multiple fields, the individual fields must be separated by commas. When specifying a sort, the default direction is ascending; to change the sort order for a given field, you use the DESC keyword after the field name. To sort contact information alphabetically by state and then by last name in a descending order, use the following SQL statement:
SELECT * FROM Contact WHERE State IN (NY, NJ) ORDER BY State,
     [Last Name] DESC
Working with SQL can be very frustrating at times, but it also makes your life as a programmer easier because of the functions it performs for you. Without SQL and relational databases, every data-access function would need to be coded within your application. This review was only for the SELECT statement and a few of the many possible actions that you can perform with SQL. In the next few lessons, you'll see how to use SQL while designing the data forms and data-access processes in your application.

 
< previous page page_249 next page >

If you like this book, buy it!